﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;
using Telerik.Web.UI;
using RN.ERP.Web.ModelService;

namespace RN.ERP.Web.Exam
{
    public partial class ScoreList : BasePage
    {
        #region Params

        private string CurPageCode = "CJ";
        private string CurPage = "ScoreList.aspx";
        private string QCPageCode = "QC";
        private string QCPage = "BmQCList.aspx";
        private string AppID
        {
            get
            {
                return HttpContext.Current.Application[ApplicationConstant.APPLICATION_ID].ToString();
            }
        }

#if DEBUG
        public override bool IsCheckPermission
        {
            get
            {
                return false;
            }
        }
#endif

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ////绑定期次下拉
                BindQCDDL();

                ControlInit();
            }
        }

        /// <summary>
        /// 控件初始化
        /// </summary>
        private void ControlInit()
        {
            if (UserInfo.RoleId == "56")
            {
                pageTitle.InnerText = "成绩查询";
                funcTitle.InnerText = "成绩查询";
            }
            else
            {

            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            BindData();
        }

        protected void ddlQC_SelectedIndexChanged(object sender, EventArgs e)
        {
            RadGrid1.CurrentPageIndex = 0;
            BindData();
            RadGrid1.DataBind();
        }

        private void BindData()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                RadGrid1.AllowCustomPaging = true;
                int pageCount = RadGrid1.CurrentPageIndex;
                int pageSize = RadGrid1.PageSize;
                int count = 0;
                int qc = 0;
                int.TryParse(ddlQC.SelectedValue, out qc);
                ////获取该期次下的成绩列表
                var score = client.GetScoreList(CurPageCode, UserInfo.RoleId, CurPage, UserInfo.CorpUserGuid, AppID, qc, pageCount + 1, pageSize, out count);
                if (score != null)
                {
                    OperationResult result = JsonConvert.DeserializeObject<OperationResult>(score);
                    if (result.Result && count > 0)
                    {
                        RadGrid1.VirtualItemCount = count;
                        RadGrid1.DataSource = result.ReturnValue;
                    }
                }
            }
        }

        private void BindQCDDL()
        {
            using (PersonService.PersonServiceClient client = new PersonService.PersonServiceClient())
            {
                int count = 0;
                var qc = JsonConvert.DeserializeObject<List<ModelService.Exam_QC>>(client.GetExamQCList
                       (QCPageCode, UserInfo.RoleId, QCPage, AppID, 0, 0, out count));
                ddlQC.DataSource = qc;
                ddlQC.DataValueField = "ID";
                ddlQC.DataTextField = "Title";
                ddlQC.DataBind();
                if (qc.Count == 0)
                {
                    ddlQC.Items.Insert(0, new ListItem("--请选择--", "0"));
                }
            }
        }
    }
}